package com.hsq.androidproject.ui.recommend_fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyGridViewStarDTAdapter;
import com.hsq.androidproject.adapter.MyGridViewStartRankingAdapter;
import com.hsq.androidproject.entity.HotStars;
import com.hsq.androidproject.entity.StarDynamic;
import com.hsq.androidproject.entity.StarDynamicTmp;
import com.hsq.androidproject.ui.ShowStarRankingActivity;
import com.hsq.androidproject.ui.fragment.BaseFragment;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class RStarFragment extends BaseFragment {
    /*明星排行版*/
    private GridView mGridViewStarRanking;
    private List<HotStars.HotStarsBean> hotStarsBeen;
    private MyGridViewStartRankingAdapter adapter;
    private LinearLayout ll_more;   /////更多
    List<StarDynamicTmp> tmps;
    /*明星动态*/
    private GridView mGridViewStarDynamic;
    private ImageView iv_img, iv_more;

    public RStarFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_rstar, container, false);
        initView(view);

        /*明星排行版*/
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/star/index", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                HotStars hotStars = new Gson().fromJson(response, HotStars.class);
                hotStarsBeen = hotStars.getHotStars();
                adapter = new MyGridViewStartRankingAdapter(hotStarsBeen, getContext());
                mGridViewStarRanking.setAdapter(adapter);
            }
        });

        Glide.with(getContext())
                .load(R.drawable.more_hot_star_icon)
                .transform(new GlideCircleTransform(getContext()))//圆形
                .into(iv_img);

        iv_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickMoreStar();
            }

        });
        ll_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickMoreStar();
            }
        });


        /*明星动态*/
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/star/hotVideos?offset=0&count=30", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                StarDynamic starDynamic = new Gson().fromJson(response, StarDynamic.class);
                tmps = new ArrayList<>();
                for (int i = 0; i < starDynamic.getVideos().size(); i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(starDynamic.getVideos().get(i).getThumb());
                    tmp.setVid(starDynamic.getVideos().get(i).getVid());
                    tmp.setTitle(starDynamic.getVideos().get(i).getTitle());
                    for (int j = 0; j < starDynamic.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(starDynamic.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(starDynamic.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0,32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getActivity());
                mGridViewStarDynamic.setAdapter(adapter);
            }
        });
        return view;
    }

    private void clickMoreStar() {
        intent2Activity(ShowStarRankingActivity.class);
    }

    private void initView(View view) {
        mGridViewStarRanking = (GridView) view.findViewById(R.id.mGridViewStarRanking);
        mGridViewStarDynamic = (GridView) view.findViewById(R.id.mGridViewStarDynamic);
        ll_more = (LinearLayout) view.findViewById(R.id.ll_more);
        iv_img = (ImageView) view.findViewById(R.id.iv_img);    ////更多
        iv_more = (ImageView) view.findViewById(R.id.iv_more);

    }
}
